package com.ruoyi.mall.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.DeleteObject;
import com.ruoyi.mall.domain.GoodsClass;
import com.ruoyi.mall.mapper.GoodsClassMapper;
import com.ruoyi.mall.service.IGoodsClassService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类Controller
 *
 * @author ruoyi
 * @date 2025-04-26
 */
@RestController
@RequestMapping("/mall/class")
public class GoodsClassController extends BaseController {
    @Autowired
    private IGoodsClassService goodsClassService;
    @Autowired
    private GoodsClassMapper goodsClassMapper;

    /**
     * 查询商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('mall:class:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsClass goodsClass) {
        goodsClass.setIsDelete(0L);
        startPage();
        List<GoodsClass> list = goodsClassService.selectGoodsClassList(goodsClass);
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('mall:class:export')")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsClass goodsClass) {
        List<GoodsClass> list = goodsClassService.selectGoodsClassList(goodsClass);
        ExcelUtil<GoodsClass> util = new ExcelUtil<GoodsClass>(GoodsClass.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:class:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(goodsClassService.selectGoodsClassById(id));
    }

    /**
     * 新增商品分类
     */
    @PreAuthorize("@ss.hasPermi('mall:class:add')")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsClass goodsClass) {
        return toAjax(goodsClassService.insertGoodsClass(goodsClass));
    }

    /**
     * 修改商品分类
     */
    @PreAuthorize("@ss.hasPermi('mall:class:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsClass goodsClass) {
        return toAjax(goodsClassService.updateGoodsClass(goodsClass));
    }

    /**
     * 删除商品分类
     */
    @PreAuthorize("@ss.hasPermi('mall:class:remove')")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
//        return toAjax(goodsClassService.deleteGoodsClassByIds(ids));
        for (Integer id : ids) {
            //查询该分类下有没有绑定商品
            DeleteObject deleteObject = goodsClassMapper.deleteObject(id);
            if (deleteObject.getCounts() > 0) {
                return error("'" + deleteObject.getName() + "'分类下存在绑定商品,请先取消绑定！");
            }
            GoodsClass object = new GoodsClass();
            object.setId(id.longValue());
            object.setIsDelete(1L);
            goodsClassService.updateGoodsClass(object);
        }
        return toAjax(1);
    }
}
